package com.open.party.cloud.view.examine

import android.os.Bundle
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.sinothk.hussars.parent.AppEventBusRecycleViewTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.*
import com.open.party.cloud.view.examine.adapter.ExamineClassesAttendanceListForTodayAdapter
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.actvity_examine_classes_ex_list_view.loadingRecyclerView
import kotlinx.android.synthetic.main.actvity_examine_classes_ex_list_view.loadingTipView
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ExamineAttendanceHistoryListActivity : AppEventBusRecycleViewTitleBaseActivity<String>() {
    var classId: String? = null
    var viewModel: ExamineViewModel? = null

    private var adapter: ExamineClassesAttendanceListForTodayAdapter? = null


    override fun getLayoutResId(): Int = R.layout.actvity_examine_classes_attendance_history_list_view

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        classId = intent.getStringExtra("classId")
//        setViewTitle(XUtils.string().getNotNullValue(examineClass!!.className, "班级试卷列表"), "成员") {
//        }
        setViewTitle("签到记录")
        titleBarView.setCenterTxtColor(R.color.page_title)

        viewModel = ExamineViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = ExamineClassesAttendanceListForTodayAdapter(this)
        loadingRecyclerView.adapter = adapter
//        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            refresh()
        }
    }

    override fun onResume() {
        super.onResume()
        refresh()
    }

    private fun refresh() {
        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<String>) {
        val pageReq = PageReq<ExamineClassVO>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize

        val vo = ExamineClassVO()
        vo.classId = classId!!
        pageReq.data = vo

        viewModel!!.getExamineClassesAttendanceListForAll(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<AttendanceEntity>>) {
        if (result.eventType != "getExamineClassesAttendanceListForAll") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<AttendanceEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }
}